package com.sunway.paisong.pos.service;

import org.apache.http.Header;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.sunway.paisong.pos.ui.EditUserActivity;
import com.sunway.paisong.pos.ui.MainActivity;
import com.sunway.paisong.pos.util.HttpUtils;
import com.sunway.paisong.pos.util.StringUtils;
import com.sunway.paisong.pos.vo.HandshakeResVO;
import com.sunway.paisong.pos.vo.JsonVO;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class ReportService extends Service {

	public static final String ACTION = "com.sunway.paisong.pos.service.ReportService";
	private SharedPreferences sp;
	private boolean isBind = false;
	private String token;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		sp = this.getSharedPreferences("com.sunway.paisong.pos.sp", MODE_PRIVATE);
		isBind = sp.getBoolean("is_bind", false);
		if (isBind) {
			token = sp.getString("token", "");
			if (!StringUtils.isBlank(token)) {
				Log.i("sw", "startReport");
				double latitude = 0.0;
				double longitude = 0.0;
				Location location = getLocation();
				Log.i("sw", "location:" + location);
				if (location != null) {
					Log.i("sw", "location:" + location);
					latitude = location.getLatitude();
					longitude = location.getLongitude();
				}
				RequestParams requestParams = new RequestParams();
				requestParams.add("token", token);
				requestParams.add("lng", longitude + "");
				requestParams.add("lat", latitude + "");
				HttpUtils.post("/mob/pos/report", requestParams, new AsyncHttpResponseHandler() {

					@Override
					public void onStart() {
						// called before request is started
						Log.i("sw", "onStart");
					}

					@Override
					public void onSuccess(int statusCode, Header[] headers, byte[] response) {
						Log.i("sw", "onSuccess:" + new String(response));
						Toast.makeText(ReportService.this, "onSuccess" + new String(response),
								Toast.LENGTH_SHORT).show();
					}

					@Override
					public void onFailure(int statusCode, Header[] headers, byte[] errorResponse,
							Throwable e) {
						Log.i("sw", "onFailure:" + new String(errorResponse));
						Toast.makeText(ReportService.this, "onFailure" + new String(errorResponse),
								Toast.LENGTH_SHORT).show();
					}

					@Override
					public void onRetry(int retryNo) {
						Log.i("sw", "onRetry");
					}

				});
			}
		}
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	class ReportThread extends Thread {
		@Override
		public void run() {

		}
	}

	private Location getLocation() {
		LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
			Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
			return location;
		} else {
			LocationListener locationListener = new LocationListener() {

				// Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
				@Override
				public void onStatusChanged(String provider, int status, Bundle extras) {
					// TODO Auto-generated method stub
				}

				// Provider被enable时触发此函数，比如GPS被打开
				@Override
				public void onProviderEnabled(String provider) {
				}

				// Provider被disable时触发此函数，比如GPS被关闭
				@Override
				public void onProviderDisabled(String provider) {
				}

				// 当坐标改变时触发此函数，如果Provider传进相同的坐标，它就不会被触发
				@Override
				public void onLocationChanged(Location location) {
					if (location != null) {
						Log.e("Map", "Location changed : Lat: " + location.getLatitude() + " Lng: "
								+ location.getLongitude());
					}
				}

			};
			locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0,
					locationListener);
			Location location = locationManager
					.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
			return location;
		}

	}
}
